<?php
	$city_obj = new city();
	$action_ary = array(
	'list' => 'city_act_list',
	'area_list' => 'city_act_area_list',
	'get_city'=>'city_act_get_city_by_name',
	'city_picker' => 'city_act_city_picker',
	'city_sort' => 'city_act_city_sort'
	);

	//执行函数
	if($action && ($fun = $action_ary[$action])){
		$fun();
	}


	/********  action  *******************/

	/**
	* 城市排序
	* @author xiezhihui 13-12-23 03:49
	*/
	function city_act_city_sort(){
		global $city_obj;
		//得到参数
		$sort = request('sort');
		$num =0;

		if(!$sort || !is_array($sort)){
			location_msg(L('1003'));
		}

		foreach($sort as $k=>$v){
			$id = $k;
			$sort = $v;
			$data =compact('id','sort');
			$city_obj->_city_sort_edit($id, $data);
			$num++;
		}
		if($num<1){
			location_msg(L('1003'));
		}
		location_msg(L('1004'));
	}
	/**
	* 前端城市选择框需要的城市列表
	* @author zhufu 2013-09-10
	*
	*/
	function city_act_city_picker(){
		global $city_obj;
		$list = $city_obj->out_list_main();
		$ipcity = $city_obj->_area()->get_ip_info();
		$rtn = array('state'=>false);
		if(!$list){
			echo_json($rtn);
		}
		$result = array('hot'=>array(),'normal'=>array());
		foreach($list as $k=>$v){
			if($v['main'] == 2){
				$result['hot'][] = $v;
			}
			else{
				$result['normal'][] = $v;
			}
		}
		$rtn = array('state'=>true, 'list'=>$result);
		echo_json($rtn);
	}


	/**
	* 根据id,得到城市信息
	* @author <zhufu159239@163.com> 2012-09-17 12:02:53
	*
	* @param int $id    记录id
	* @return array
	*/
	function city_fun_info($id){
		global $city_obj;
		//检查参数
		if(!$id){
			return '';
		}
		return $city_obj->out_info($id);
	}

	/**
	* js请求城市列表
	* @author <zhufu159239@163.com> 2012-07-25 16:37:39
	*
	*/
	function city_act_list(){
		global $city_obj;
		//得到参数
		$pid = request('pid', false, true, false, 0);
		$list = $city_obj->out_list($pid);
		//组织数据
		$rtn = array('state'=>false);
		if(!$list){
			exit(json_encode($rtn));
		}
		$rtn['state'] = true;
		$rtn['msg'] = $list;
		echo json_encode($rtn);
	}

	/**
	* js请求城市区域列表
	* @author limin  2013-4-28    PM 14:21:00
	*
	*/
	function city_act_area_list(){
		global $city_obj;
		//城市ID
		$id = request('id', false, true, false, 0);
		//区域等级
		$level=request('level',false,true);
		$list = $city_obj->_city_list($id,$level);
		//组织数据
		$rtn = array('state'=>false);
		if(!$list){
			exit(json_encode($rtn));
		}
		foreach($list as $k=>$v){
			$list[$k]['letter'] = $v['spelling'] ? strtoupper(substr($v['spelling'],0,1)) : '';
		}
		$rtn['state'] = true;
		$rtn['msg'] = $list;
		echo json_encode($rtn);
		exit;
	}

	/**
	* 根据城市名称获取城市信息
	* @author limin  2013-5-6  PM 16:45:00
	*/
	function city_act_get_city_by_name(){
		global $city_obj;
		//城市名称
		$name=request('name');
		$info=$city_obj->_city_by_name($name);
		//组织数据
		$rtn = array('state'=>false);
		if(!$info){
			exit(json_encode($rtn));
		}
		$rtn['state'] = true;
		$rtn['msg'] = $info;
		echo json_encode($rtn);
	}


	/********  function  **********************/

	/**
	* 根据城市名称获取城市信息
	* @author limin  2013-5-6  PM 16:45:00
	* @param  string $name  城市名
	*/
	function city_fun_get_city_by_name($name){
		global $city_obj;
		if(!$name){
			return false;
		}
		return $city_obj->_city_by_name($name);
	}


	/**
	* 根据城市ID和区域级别获得城市信息
	* @author limin 2013-5-3  PM 16:00:00
	* @param mixed $id 城市id
	*/
	function city_fun_area_list($id){
		global $city_obj;
		if(!$id){
			return false;
		}
		return $city_obj->_area_list($id);
	}

	/**
	* 返回直辖市的id数组
	*
	*/
	function xz_city(){
		global $city_obj;
		return $city_obj->zx_city();
	}

	/**
	* 根据ID获得城市信息
	* @author limin 2013-5-3    PM 16:33:00
	* @param mixed $id  城市id
	*/
	function city_fun_area_info($id){
		global $city_obj;
		if(!$id){
			return false;
		}
		return $city_obj->_city_info($id);
	}

	/**
	* 得到城市列表
	* @author <zhufu159239@163.com> 2012-07-25 16:33:56
	*
	* @param int $pid   上级id,默认（0）为得到一级城市
	*/
	function city_fun_list($pid=0){
		global $city_obj;
		return $city_obj->out_list_2($pid);
	}

	function city_fun_city_list($pid=0,$level=0,$sort="`spelling` ASC"){
		global $city_obj;
		return $city_obj->_city_list($pid,$level,$sort);
	}

	/**
	* 得到城市列表,之前的ID为自动递增没有级别信息，不能用
	* @author zhufu 2013-07-26
	*
	* @param int $pid 上级ID
	* @return array
	*/
	function city_fun_city_list_2($pid=0,$child=0){
		global $city_obj;
		return $city_obj->out_list_2($pid,$child);
	}


	/**
	* 根据拼音首字母获得城市信息
	* @author limin 2013-5-6  PM 13:19:00
	*/
	function city_fun_get_city_by_spell(){
		global $city_obj, $_site;
		$letters=range('a','z');
		//如果缓存文件不存在则创建
		if(!$city_list){
			foreach($letters as $k=>$v){
				$cities=$city_obj->_get_cities_by_spell($v);
				$city_list[$v]=$cities;
			}
		}
		return $city_list;
	}

	/**
	* 根据拼音缩写获得单个城市信息
	* @author limin  PM 2013-5-13 14:05:34
	* @param  string $spell  拼音
	*/
	function city_fun_get_city_info($spell){
		global $city_obj;
		if(!$spell){
			return false;
		}
		return $city_obj->_city_by_spell($spell);
	}

	/**
	*获得域名地址判断是否进行跳转
	*@author limin 2013-9-24 AM 10:29:00
	*@param string  $domain 根域名
	*@param  int  $city_id 小区的城市ID
	*@param  int $cid 小区ID
	*/
	function city_fun_get_domain_url($domain,$city_id=0,$cid=0){
		global $city_obj;
		if(!$domain){
			return false;
		}
		$domains=explode('.',$domain);
		$count=count($domains);
		switch($count){
			case 2:
				$d_str1=$domains[0];
				$d_str2=$domains[1];
				break;
			case 3:
				//城市信息
				$city_info=city_fun_get_city_info($domains[0]);
				$d_str1=$domains[1];
				$d_str2=$domains[2];
				break;
		}
		//传入了小区城市ID  则获取小区城市进行判断跳转
		if($city_id&&($city_info['id']!=$city_id)&&$cid){
			$city_info=city_fun_info($city_id);
			if($city_info['spelling']){
				header("Location:http://{$city_info['spelling']}.{$d_str1}.{$d_str2}/xiaoqu/index?cid={$cid}");
				exit;
			}
		}
		//没有城市信息  则跳转当前城市首页
		if(!$city_info){
			$url=cur_url();
			$ary=explode('/',$url);
			unset($ary[0]);
			unset($ary[1]);
			unset($ary[2]);
			$url=implode('/',$ary);
			$city = get_city_by_ip();
			$city_info=city_fun_get_city_by_name($city);
			if($city_info['spelling']){
				header("Location:http://{$city_info['spelling']}.{$d_str1}.{$d_str2}/{$url}");
				exit;
			}
		}

	}


?>
